﻿using FastCloud.Base;
using FastCloud.Base.DB;
using FastIOC.Attribute;
using FastORM.Entity;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.Base
{
    [Route("frame/base/log")]
    public class LogController : BaseController
    {
        [AutoWired]
        private DB_VisitLog DB_VisitLog;
        [AutoWired]
        private DB_OperationLog DB_OperationLog;

        [Route("visitlist")]
        public IActionResult VisitLog()
        {
            return View("~/Views/Base/Log/VisitLog_List.cshtml");
        }

        [Route("operatelist")]
        public IActionResult OperateLog()
        {
            return View("~/Views/Base/Log/OperateLog_List.cshtml");
        }

        [Route("getVisitLogList")]
        public ActionResult GetVisitLogList()
        {
            string StartDate = ValueConvert.ConvertToString(GetFormParam("StartDate"));
            string EndDate = ValueConvert.ConvertToString(GetFormParam("EndDate"));
            string sql = "1=1";
            List<ParamEntity> ParamList = new List<ParamEntity>();
            if (!string.IsNullOrEmpty(StartDate))
            {
                sql += " and DATE_FORMAT(VisitDateTime,'%Y-%m-%d')>=DATE_FORMAT(@StartDate,'%Y-%m-%d')";
                ParamList.Add(new ParamEntity() { ParamName = "StartDate", ParamValue = Convert.ToDateTime(StartDate).ToString("yyyy-MM-dd") });
            }
            if (!string.IsNullOrEmpty(EndDate))
            {
                sql += " and DATE_FORMAT(VisitDateTime,'%Y-%m-%d')<=DATE_FORMAT(@EndDate,'%Y-%m-%d')";
                ParamList.Add(new ParamEntity() { ParamName = "EndDate", ParamValue = Convert.ToDateTime(EndDate).ToString("yyyy-MM-dd") });
            }
            return Result.OK(DB_VisitLog.SelectPage("*", sql, "VisitDate desc", ParamList));
        }


        [Route("getOperateLogList")]
        public ActionResult GeetOperateLogList()
        {
            string StartDate = ValueConvert.ConvertToString(GetFormParam("StartDate"));
            string EndDate = ValueConvert.ConvertToString(GetFormParam("EndDate"));
            string sql = "1=1";
            List<ParamEntity> ParamList = new List<ParamEntity>();
            if (!string.IsNullOrEmpty(StartDate))
            {
                sql += " and DATE_FORMAT(OperateDate,'%Y-%m-%d')>=DATE_FORMAT(@StartDate,'%Y-%m-%d')";
                ParamList.Add(new ParamEntity() { ParamName = "StartDate", ParamValue = Convert.ToDateTime(StartDate).ToString("yyyy-MM-dd") });
            }
            if (!string.IsNullOrEmpty(EndDate))
            {
                sql += " and DATE_FORMAT(OperateDate,'%Y-%m-%d')<=DATE_FORMAT(@EndDate,'%Y-%m-%d')";
                ParamList.Add(new ParamEntity() { ParamName = "EndDate", ParamValue = Convert.ToDateTime(EndDate).ToString("yyyy-MM-dd") });
            }
            return Result.OK(DB_OperationLog.SelectPage("*", sql, "OperateDate desc", ParamList));
        }
    }
}
